package com.heima.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Autowired
    HttpSession session;

    @Override
    public void insertFill(MetaObject metaObject) {
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        Long id = (Long)session.getAttribute("employee");
        Long user = (Long) session.getAttribute("user");
        if(id != null) {
            metaObject.setValue("createUser", id);
            metaObject.setValue("updateUser", id);
            return;
        }
        metaObject.setValue("createUser", user);
        metaObject.setValue("updateUser", user);

        log.info("公共新增完成");
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Long id = (Long)session.getAttribute("employee");
        Long user = (Long) session.getAttribute("user");
        metaObject.setValue("updateTime", LocalDateTime.now());
        if(id != null) {
            metaObject.setValue("updateUser", id);
            return;
        }
        metaObject.setValue("createUser", user);
        log.info("公共更新完成");
    }
}
